package cn.imvc.app.controllers.info;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Bank;
import cn.imvc.app.models.BankModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class BankControllerImpl implements BankController {
	static Logger logger = Logger.getLogger(BankControllerImpl.class);
	public Page<Bank> page;
	public ViewRequest viewRequest;
	public String search_content;
	public Bank bank;
	public int id;
	public int[] ids;
	
	@Autowired
	BankModel bankModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {  
		return null;
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
		}
		page = bankModel.find(viewRequest);
		return "bank/index";
	}

	public String show() {
		
		return null;
	}

	public String editNew() {
		
		return "bank/edit";
	}

	public String create() {
		int result = bankModel.save(bank);
		if(result==0){
			return Render.json(new DialogBean("300","银行编码不能重复","bank","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","银行名称不能重复","bank","closeCurrent"));
		}
		loggingModel.info(logger,"添加银行："+bank.getName());
		return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
	}

	public String edit() {
		bank = bankModel.get(id);
		return "bank/edit";
	}

	public String update() {
		int result = bankModel.update(id,bank);
		if(result==0){
			return Render.json(new DialogBean("300","银行编码不能重复","bank","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","银行名称不能重复","bank","closeCurrent"));
		}
		loggingModel.info(logger,"修改银行："+bank.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		bankModel.delete(id);
		loggingModel.info(logger,"删除银行："+id);
		return Render.json(new DialogBean("200","删除成功",""));
	}

	public String destroyBatch() {
		bankModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功",""));
	}

	public Page<Bank> getPage() {
		return page;
	}

	public void setPage(Page<Bank> page) {
		this.page = page;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public Bank getBank() {
		return bank;
	}

	public void setBank(Bank bank) {
		this.bank = bank;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}



}
